package cn.lan.generator.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.lan.generator.bean.EntityAttrInfo;
import cn.lan.generator.bean.EntityInfo;
import cn.lan.generator.bean.Module;
import cn.lan.generator.bean.ProjInfo;
import cn.lan.generator.service.CreateFile;
import cn.lan.generator.utils.ConfigUtil;
import cn.lan.generator.utils.Constant;
import cn.lan.generator.utils.FileUtil;
import cn.lan.generator.utils.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lanping
 * @version 1.0
 * @date 2019-07-06
 **/
public class CreateEntityDtoImpl implements CreateFile {

    @Override
    public void writeFile(EntityInfo entityInfo, ProjInfo projInfo) throws Exception {
        String packageName =  ConfigUtil.getDtoPackage(projInfo);
        String commonBeanPackage = ConfigUtil.getCommonBeanPackage(projInfo.getPackageName());
        String srcFilePath = FileUtil.getResourceRootDir(projInfo.isDebugModel())+Constant.TEMPLATE_ENTITY_DTO_PATH;

        String suffixPath = packageName.replaceAll("\\.", "\\\\") + "\\" + entityInfo.getEntityName() + "Dto" + Constant.FILE_SUFFIX_JAVA;
        String destFilePath = Constant.getFilePath(projInfo, Module.getModule(projInfo).getService(),  Constant.PathType.basePath, suffixPath);

        List<EntityAttrInfo> entityAttrs = new ArrayList<>();
        String entityAttributes= getEntityAttributes(entityInfo,entityAttrs);
        String entityFuncs= getFuncs(entityAttrs);
        Map<String,String> map = new HashMap<String,String>(){
            {
                put("filePackageName",packageName);
                put("tableComment",entityInfo.getTableComment());
                put("projectAuthor",projInfo.getAuthor());
                put("upperEntityName", entityInfo.getEntityName());
                put("commonBeanPackage", commonBeanPackage);
                put("nowDate", DateUtil.today());
                put("entityAttributes", entityAttributes);
                put("entityFuncs", entityFuncs);
            }
        };
        FileUtil.copyFile(projInfo,srcFilePath, destFilePath, map);
    }

    /**
     * 获取属性
     * @param entityInfo
     * @return
     */
    private String getEntityAttributes(EntityInfo entityInfo,List<EntityAttrInfo> entityAttrs){
        StringBuffer sb = new StringBuffer();

        for (EntityAttrInfo entityAttrInfo:entityInfo.getEntityAttrs()){
            String attrName = entityAttrInfo.getAttrName();
            String attrType = entityAttrInfo.getAttrType();
            String colname = entityAttrInfo.getColumnName();
            String columnComment = entityAttrInfo.getColumnComment();

            if("String".equals(entityAttrInfo.getAttrType()) && StringUtil.isLikeInArray(colname,Constant.SQL_LIKE_NAME_ARRAY)){
                sb.append("\t@ApiModelProperty(value = \""+columnComment+"（模糊搜索）\")\n");
                sb.append("\tprivate "+attrType+" "+attrName+"Like;\n\n");
                EntityAttrInfo entityAttrInfoNew = new EntityAttrInfo(attrName+"Like", "String");
                entityAttrs.add(entityAttrInfoNew);
            }

            if(!StringUtil.isLikeInArray(colname, Constant.QUERY_NOT_COND_ARRAY)){
                sb.append("\t@ApiModelProperty(value = \""+columnComment+"\")\n");
                sb.append("\tprivate "+attrType+" "+attrName+";\n\n");
                EntityAttrInfo entityAttrInfoNew = new EntityAttrInfo(attrName, entityAttrInfo.getAttrType());
                entityAttrs.add(entityAttrInfoNew);
            }

            if(ObjectUtil.isNotEmpty(colname) && "date".equalsIgnoreCase(entityAttrInfo.getDataType())){
                sb.append("\t@ApiModelProperty(value = \""+columnComment+"-开始（yyyy-MM-dd）\")\n");
                sb.append("\tprivate "+attrType+" "+attrName+"Start;\n\n");
                sb.append("\t@ApiModelProperty(value = \""+columnComment+"-截止（yyyy-MM-dd）\")\n");
                sb.append("\tprivate "+attrType+" "+attrName+"End;\n\n");

                EntityAttrInfo entityAttrInfoNew = new EntityAttrInfo(attrName+"Start", "String");
                entityAttrs.add(entityAttrInfoNew);

                EntityAttrInfo entityAttrInfoNew2 = new EntityAttrInfo(attrName+"End","String");
                entityAttrs.add(entityAttrInfoNew2);
            }

            if(ObjectUtil.isNotEmpty(colname) && "datetime".equalsIgnoreCase(entityAttrInfo.getDataType())){
                sb.append("\t@ApiModelProperty(value = \""+columnComment+"-开始（yyyy-MM-dd HH:mm:ss）\")\n");
                sb.append("\tprivate "+attrType+" "+attrName+"Start;\n\n");
                sb.append("\t@ApiModelProperty(value = \""+columnComment+"-截止（yyyy-MM-dd HH:mm:ss）\")\n");
                sb.append("\tprivate "+attrType+" "+attrName+"End;\n\n");

                EntityAttrInfo entityAttrInfoNew = new EntityAttrInfo(attrName+"Start", "String");
                entityAttrs.add(entityAttrInfoNew);

                EntityAttrInfo entityAttrInfoNew2 = new EntityAttrInfo(attrName+"End","String");
                entityAttrs.add(entityAttrInfoNew2);
            }

            if(attrName.equals(entityInfo.getPrimaryKey())){
                sb.append("\t@ApiModelProperty(value = \"不包含"+columnComment+"\")\n");
                sb.append("\tprivate "+attrType+" no"+StringUtil.toUpperCaseFirstOne(attrName)+";\n\n");

                EntityAttrInfo entityAttrInfoNew = new EntityAttrInfo("no"+StringUtil.toUpperCaseFirstOne(attrName),entityAttrInfo.getAttrType());
                entityAttrs.add(entityAttrInfoNew);

                sb.append("\t@ApiModelProperty(value = \"多个"+columnComment+",逗号(,)分隔\")\n");
                sb.append("\tprivate String "+attrName+"s;\n\n");

                EntityAttrInfo entityAttrInfoMore = new EntityAttrInfo(attrName+"s","String");
                entityAttrs.add(entityAttrInfoMore);
            }
        }

        return sb.toString();
    }

    /**
     * 设置get与set方法
     *
     * @param entityAttrInfos
     * @return
     */
    private String getFuncs(List<EntityAttrInfo> entityAttrInfos) {
        StringBuffer sb = new StringBuffer();
        for (EntityAttrInfo entityAttrInfo:entityAttrInfos){
            String lowerName = entityAttrInfo.getAttrName();
            String upperName = StringUtil.toUpperCaseFirstOne(entityAttrInfo.getAttrName());
            sb.append("\tpublic "+entityAttrInfo.getAttrType()+" get"+upperName+"() {\n");
            sb.append("\t\treturn "+lowerName+";\n");
            sb.append("\t}\n\n");

            sb.append("\tpublic void set"+upperName+"("+entityAttrInfo.getAttrType()+" " + lowerName+") { \n");
            sb.append("\t\tthis."+lowerName+" = "+ lowerName+";\n");
            sb.append("\t}\n\n");
        }
        return sb.toString();
    }
}
